﻿@page "/"
@using Azure.AI.OpenAI
@inject OpenAIClient aiClient
@inject ILogger<Home> logger
@inject IConfiguration configuration

    <div class="storybox" style="margin: 25%">
        @if (chatCompletionOptions != null)
        {
            foreach (var message in chatCompletionOptions.Messages.OfType<ChatRequestAssistantMessage>())
            {
                <p style="font-size: 3em;">@message.Content</p>
            }
        }

        <button @onclick="GenerateNextParagraph" autofocus>Generate</button>
    </div>

@code {
    private ChatCompletionsOptions? chatCompletionOptions = null;

    private async Task GenerateNextParagraph()
    {
        if (chatCompletionOptions == null)
        {
            chatCompletionOptions = new ChatCompletionsOptions()
            {
                DeploymentName = configuration["OpenAI:DeploymentName"] ?? throw new ApplicationException(),
                Messages =
                {
                    new ChatRequestSystemMessage("Pick a random topic and write a sentence of a fictional story about it.")
                }
            };
        }

        if (chatCompletionOptions.Messages.Count > 1)
        {
            chatCompletionOptions.Messages.Add(
                new ChatRequestUserMessage("Write the next sentence in the story.")
            );
        }

        var response = await aiClient.GetChatCompletionsAsync(chatCompletionOptions);
        var x = new ChatRequestAssistantMessage(response.Value.Choices[0].Message);
        chatCompletionOptions.Messages.Add(x);

        this.StateHasChanged();
    }

    protected override async Task OnInitializedAsync()
    {
        await GenerateNextParagraph();
        await base.OnInitializedAsync();
    }
}
